﻿using System;
using System.Threading.Tasks;


namespace CallViaPhone
{
    class Program
    {    
        static void Main(string[] args)
        {            
            Console.WriteLine("--------------------------------------------------------");
            Console.WriteLine("Enter your SIP-URI");
            String userSIPURI = Console.ReadLine();
            Console.WriteLine("Enter your password");
            String password = ReadPassword();            
            
            Console.WriteLine("Enter the FROM phone number");
            String from = Console.ReadLine();
            Console.WriteLine("Enter the TO phone number");
            String to = Console.ReadLine();
            
            RunCallViaPhone(from, to, userSIPURI, password);            
            
            Console.WriteLine("press any key to exit");
            Console.ReadLine();                      
        }

        private static void RunCallViaPhone(String from, String to, String userSIPURI, String password)
        {
            CallViaPhone call = new CallViaPhone(userSIPURI, password, from, to);            

            try
            {
                call.prepareEndpoint();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            try
            {
                call.startCall();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

        }
         
        public static string ReadPassword()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }

                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        password = password.Substring(0, password.Length - 1);
                        int pos = Console.CursorLeft;
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        Console.Write(" ");
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            Console.WriteLine();
            return password;
        }
    }
}

